<template>
  <div class="mod-discount">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :option="tableOption"
      @search-change="searchChange"
      @selection-change="selectionChange"
      @refresh-change="refreshChange"
      @on-load="getDataList"
    >
      <template slot="menuLeft"></template>

      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 0" size="small">{{$t('seckill.close')}}</el-tag>
        <el-tag
          v-else-if="scope.row.status === 1"
          type="success"
          size="small"
        >{{$t('seckill.open')}}</el-tag>
        <el-tag
          v-if="scope.row.status === 2"
          size="small"
          type="danger"
        >{{$t('groups.offlineViolation')}}</el-tag>
        <el-tag
          v-if="scope.row.status === 3"
          size="small"
          type="warning"
        >{{$t('product.pendingReview')}}</el-tag>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          icon="el-icon-edit"
          size="small"
          @click="showDiscountInfoHandle(scope.row.discountId)"
        >{{$t('discount.viewActivities')}}</el-button>
        <el-button
          type="text"
          icon="el-icon-bottom"
          size="small"
          v-if="isAuth('platform:discount:update') && scope.row.status<2"
          @click="offlineDiscountHandle(scope.row)"
        >{{$t('groups.offShelfActivities')}}</el-button>
        <el-button
          type="text"
          icon="el-icon-document-checked"
          size="small"
          v-if="isAuth('platform:discount:update') && scope.row.status > 1"
          @click="discountAuditHandle(scope.row.discountId)"
        >{{scope.row.status === 2?$t("coupon.violationOffShelf"):$t("coupon.checkPending")}}</el-button>
        <el-button
          type="text"
          icon="el-icon-view"
          size="small"
          @click="showProds(scope.row.discountId)"
        >{{$t('prodList.lookOver')}}</el-button>
        <el-button
          type="text"
          icon="el-icon-delete"
          size="small"
          style="color:red"
          @click="deleteHandle(scope.row.discountId)"
        >{{$t('remindPop.delete')}}</el-button>
      </template>
    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate"></add-or-update>
    <discount-info
      v-if="discountInfoVisible"
      ref="discountInfo"
      @refreshSelectProds="refreshChange"
    ></discount-info>
    <!-- 商品审核弹窗 -->
    <DiscountAudit
      v-if="discountAuditVisible"
      ref="discountAudit"
      selectUrl="/platform/discount/getOfflineHandleEventByDiscountId"
      updateUrl="/platform/discount/auditDiscount"
      @refreshDataList="refreshChange"
    ></DiscountAudit>
  </div>
</template>

<script>
import AddOrUpdate from './discountProd-add-or-update'
import DiscountInfo from './discountInfo'
import { tableOption } from '@/crud/marketing/discount'
import DiscountAudit from '@/components/offline-audit'
export default {
  data () {
    return {
      dataForm: {
        discountName: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      dataListLoading: false,
      discountAuditVisible: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      discountInfoVisible: false,
      tableOption: tableOption

    }
  },
  components: {
    AddOrUpdate,
    DiscountInfo,
    DiscountAudit
  },
  methods: {
    // 获取数据列表
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/discount/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增
    addOrUpdateHandle (id) {
      this.$router.push({ path: '/discountInfo', query: { discountId: id } })
    },
    // 查看商品
    showProds (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.showProd(id, 1)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('scoreProdList.isDelete'), this.$i18n.t('remindPop.remind'), {
        confirmButtonText: this.$i18n.t('remindPop.confirm'),
        cancelButtonText: this.$i18n.t('remindPop.cancel'),
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl(`/platform/discount/${id}`),
            method: 'delete',
            data: this.$http.adornData({}, false)
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        })
        .catch(() => { })
    },
    // 条件查询
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 多选变化
    selectionChange (val) {
      this.dataListSelections = val
    },
    showDiscountInfoHandle (id) {
      this.discountInfoVisible = true
      this.$nextTick(() => {
        this.$refs.discountInfo.init(id)
      })
    },
    // 更新数据
    refreshChange () {
      this.getDataList()
    },
    // 下架活动
    offlineDiscountHandle (row) {
      // '确定对活动[' + row.discountName + ']进行下架操作？如是，请填写下架理由'
      var tips = this.$i18n.t('seckill.offlineTip')
      tips = tips.replace('ACTIVITYNAME', row.discountName)
      this.$prompt(tips, this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel')
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error(this.$i18n.t('remindPop.emptyReason'))
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/discount/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.discountId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    // 弹窗处理
    discountAuditHandle (id) {
      this.discountAuditVisible = true
      this.$nextTick(() => {
        this.$refs.discountAudit.init(id)
      })
    }
  }
}
</script>
